<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Shop extends Controller_Admin_Main
{

    private $shop;

    public function before()
    {
        parent::before();
        $this->shop = Model::factory('shop_goods');
    }

    public function action_list()
    {
        $this->display_list($this->shop->get_base_list());
    }

    public function action_search()
    {
        $data = array();
        if ($_POST['category_id'] != '')
        {
            $data['category_id'] = $_POST['category_id'];
        }

        if ($_POST['is_onsale'] != '')
        {
            $data['is_onsale'] = $_POST['is_onsale'];
        }

        if ($_POST['is_lock'] != '')
        {
            $data['is_lock'] = $_POST['is_lock'];
        }

        if ($_POST['keywords'] != '')
        {
            $data['name'] = $_POST['keywords'];
        }
        $this->display_list($this->shop->get_base_list($data));
    }

    public function display_list($goods_list)
    {
        //缓存的商品分类--用于搜索
        $this->template->assign('category_option', $this->get_category_cache_option('goods'));
        $this->template->assign('goods_list', $goods_list['list']);
        $this->template->assign('page', $goods_list['page']);
        $this->template->view('goods_list');
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        $goods_detail = $this->shop->get_goods_detail($id);
        //获取产品的推荐位
        $recommend_list = Model::factory('recommend')->get_recommends_id($id,'goods');
        //获取推荐位类别
        $recommend_category = $this->get_category_cache_checkbox('recommend', SYSTEM_SHOP_RECOMMEND_CATEGORY, $recommend_list, 'recommend[]');
        $this->template->assign('checkboxs', $recommend_category);
        if ($_POST)
        {
            $recommend_arr = isset($_POST['recommend']) ? $_POST['recommend'] : array();
            if (is_array($recommend_arr))
            {
                Model::factory('recommend')->add_recommend($id, $recommend_arr, 'goods');
                Message::success(__('update success'));
                $this->request->redirect('admin/shop/edit/' . $id);
            }
        }
      
        $this->template->assign('data', $goods_detail);
        $this->template->view('goods_edit');
    }

    public function action_lock()
    {
        $id = $this->request->param('id');
        if ($this->shop->update(array('is_lock' => 1), array('id' => $id)))
        {
            Message::success(__('lock success'));
            $this->request->redirect(Route::get('admin/shop')->uri(array('action' => 'list')));
        }
    }

    public function action_unlock()
    {
        $id = $this->request->param('id');
        if ($this->shop->update(array('is_lock' => 0), array('id' => $id)))
        {
            Message::success(__('unlock success'));
            $this->request->redirect(Route::get('admin/shop')->uri(array('action' => 'list')));
        }
    }

    public function action_delete()
    {
        $id = $this->request->param('id');
        if ($this->shop->delete(array('id' => $id)))
        {
            Message::success(__('delete success'));
            $this->request->redirect(Route::get('admin/shop')->uri(array('action' => 'list')));
        }
    }

}

